/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frm.billingAndPaymentProcessing.managedbean;

import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import frm.billingAndPaymentProcessing.entity.IncomingBillEntity;
import frm.billingAndPaymentProcessing.entity.VoucherEntity;
import frm.billingAndPaymentProcessing.session.IncomingBillSessionBean;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.event.ToggleEvent;
import scm.rawMaterialPurchasing.entity.RawMaterialPurchasingOrderEntity;
import scm.rawMaterialPurchasing.entity.RawMaterialPurchasingOrderLineItemEntity;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class ManageIncomingBillManagedBean {
private IncomingBillEntity incomingBillRecord;
private String message;
private String displayContent;
private double payment;
private VoucherEntity voucher;
private boolean paymentNeed = false;
private boolean voucherHas =false;
private boolean isDue = false;
private String employeeName;
private RawMaterialPurchasingOrderEntity rawMaterialPurchasingOrderEntity;
private List<RawMaterialPurchasingOrderLineItemEntity> rmLineItemList;
@EJB
IncomingBillSessionBean incomingBillSessionBean;
@EJB
EmployeeSessionBean employeeSessionBean;
    /** Creates a new instance of ManageIncomingBillManagedBean */
    public ManageIncomingBillManagedBean() {
//        incomingBillRecord = new IncomingBillEntity();
        voucher = new VoucherEntity();
    }

    public void initView(PhaseEvent event) {
        if (incomingBillRecord == null) {
            incomingBillRecord = (IncomingBillEntity) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("incomingBillEntity");
        }
        if (message == null) {
            message = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("message").toString();
        }
        if (message.equals("IncomingBillCreation")) {
            displayContent = "Incoming Bill Record Has Been Created";
        }
        if (incomingBillRecord.getAmountPayable() > 0) {
            paymentNeed = true;
        }
        if (incomingBillRecord.getVoucher() != null) {
            voucherHas = true;
        }
        isDue = checkDate();
        employeeName =employeeSessionBean.getEmployeeById(incomingBillRecord.getCreatedBy()).getName();
        rawMaterialPurchasingOrderEntity = incomingBillRecord.getRawMaterialPurchasingOrder();
        rmLineItemList = rawMaterialPurchasingOrderEntity.getRawMaterialPurchasingOrderLineItems();
    }

    public void createVoucher() throws IOException {
        if (incomingBillRecord.getVoucher() == null) {
            if (payment <= incomingBillRecord.getTotalDue()) {
                voucher.setPaymentDate(Calendar.getInstance().getTime());
                voucher.setAmountPaid(payment);
                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
                Long employeeId = (Long) request.getSession().getAttribute("userId");
                voucher.setIssuedBy(employeeId);
                FacesContext.getCurrentInstance().getExternalContext().getFlash().put("voucher", voucher);
                FacesContext.getCurrentInstance().getExternalContext().getFlash().put("incomingBillRecord", incomingBillRecord);
                FacesContext.getCurrentInstance().getExternalContext().redirect("voucherConfirmation.xhtml");
            }
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Payment Amount Exceeds Total Due, Please Revise the Number", ""));
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Vourcher Has Been Created", ""));
    }

    private Boolean checkDate() {
        boolean check = false;
        Date todayDate = Calendar.getInstance().getTime();
        if (incomingBillRecord.getDueDate().before(todayDate)) {
            check = true;
        }
        return check;
    }
  public List<RawMaterialPurchasingOrderEntity> formRMPOrderList() {
        List<RawMaterialPurchasingOrderEntity> rmList = new ArrayList<RawMaterialPurchasingOrderEntity>();
        rmList.add(rawMaterialPurchasingOrderEntity);
        return rmList;
    }     


    public void abolishIncomingBill() {

        incomingBillRecord.setAbolished(true);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Invoice is Successfully aboloshed", ""));
        incomingBillSessionBean.saveChanges(incomingBillRecord);

        //******************************display the result
    }

    public void viewAll() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect("viewAllIncomingBills.xhtml");
    }
    
    public void goView(ActionEvent event)throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("voucher",incomingBillRecord.getVoucher());
        FacesContext.getCurrentInstance().getExternalContext().redirect("displayVoucher.xhtml");
    }

    public IncomingBillEntity getIncomingBillRecord() {
        return incomingBillRecord;
    }
    public void handleToggle(ToggleEvent event) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Fieldset Toggled", "Visibility:" + event.getVisibility());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    public void setIncomingBillRecord(IncomingBillEntity incomingBillRecord) {
        this.incomingBillRecord = incomingBillRecord;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDisplayContent() {
        return displayContent;
    }

    public void setDisplayContent(String displayContent) {
        this.displayContent = displayContent;
    }

    public double getPayment() {
        return payment;
    }

    public void setPayment(double payment) {
        this.payment = payment;
    }

    public VoucherEntity getVoucher() {
        return voucher;
    }

    public void setVoucher(VoucherEntity voucher) {
        this.voucher = voucher;
    }

    public boolean isPaymentNeed() {
        return paymentNeed;
    }

    public void setPaymentNeed(boolean paymentNeed) {
        this.paymentNeed = paymentNeed;
    }

    public boolean isVoucherHas() {
        return voucherHas;
    }

    public void setVoucherHas(boolean voucherHas) {
        this.voucherHas = voucherHas;
    }

    public boolean isIsDue() {
        return isDue;
    }

    public void setIsDue(boolean isDue) {
        this.isDue = isDue;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public List<RawMaterialPurchasingOrderLineItemEntity> getRmLineItemList() {
        return rmLineItemList;
    }

    public void setRmLineItemList(List<RawMaterialPurchasingOrderLineItemEntity> rmLineItemList) {
        this.rmLineItemList = rmLineItemList;
    }
}
